<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="专业类别名称">
          <a-input placeholder="请输入专业类别名称" v-decorator="[ 'specialtyName', validatorRules.specialtyName]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="专业类别编码">
          <a-input placeholder="请输入专业类别编码" :disabled="specialtyDisabled" v-decorator="[ 'specialtyCode', validatorRules.specialtyCode]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="描述">
          <a-textarea :rows="5" placeholder="..." v-decorator="[ 'description', validatorRules.description ]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {addSpecialty,editSpecialty,duplicateCheck } from '@/api/specialty'

  export default {
    name: "SpecialtyModal",
    data () {
      return {
        title:"操作",
        visible: false,
        specialtyDisabled: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          specialtyName:{
              rules: [
                { required: true, message: '请输入专业类别名称!' },
                { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
              ]},
            specialtyCode:{
              rules: [
                { required: true, message: '请输入专业类别编码!'},
                { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
                { validator: this.validateSpecialtyCode}
              ]},
            description:{
              rules: [
                { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
              ]}
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;

        //编辑页面禁止修改专业类别编码
        if(this.model.id){
          this.specialtyDisabled = true;
        }else{
          this.specialtyDisabled = false;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'specialtyName', 'specialtyCode','description'))
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            let obj;
            console.log(formData)
            if(!this.model.id){
              obj=addSpecialty(formData);
            }else{
              obj=editSpecialty(formData);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateSpecialtyCode(rule, value, callback){
        if(/[\u4E00-\u9FA5]/g.test(value)){
          callback("专业类别编码不可输入汉字!");
        }else{
          var params = {
            tableName: "sys_specialty",
            fieldName: "specialty_code",
            fieldVal: value,
            dataId: this.model.id,
          };
          duplicateCheck(params).then((res)=>{
            if(res.success){
              callback();
            }else{
              callback(res.message);
            }
          });
        }
      }
    }
  }
</script>

<style scoped>

</style>